package com.sugar.follow.up.module.business.controller.admin.user;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.follow.up.module.business.controller.admin.user.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.user.BizUserDO;
import com.sugar.follow.up.module.business.service.user.BizUserService;

@Tag(name = "管理后台 - 用户")
@RestController
@RequestMapping("/business/biz-user")
@Validated
public class BizUserController {

    @Resource
    private BizUserService bizUserService;

    @PostMapping("/create")
    @Operation(summary = "创建用户")
    @PreAuthorize("@ss.hasPermission('business:biz-user:create')")
    public CommonResult<Long> createBizUser(@Valid @RequestBody BizUserSaveReqVO createReqVO) {
        return success(bizUserService.createBizUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户")
    @PreAuthorize("@ss.hasPermission('business:biz-user:update')")
    public CommonResult<Boolean> updateBizUser(@Valid @RequestBody BizUserSaveReqVO updateReqVO) {
        bizUserService.updateBizUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:biz-user:delete')")
    public CommonResult<Boolean> deleteBizUser(@RequestParam("id") Long id) {
        bizUserService.deleteBizUser(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户")
                @PreAuthorize("@ss.hasPermission('business:biz-user:delete')")
    public CommonResult<Boolean> deleteBizUserList(@RequestParam("ids") List<Long> ids) {
        bizUserService.deleteBizUserListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:biz-user:query')")
    public CommonResult<BizUserRespVO> getBizUser(@RequestParam("id") Long id) {
        BizUserDO bizUser = bizUserService.getBizUser(id);
        return success(BeanUtils.toBean(bizUser, BizUserRespVO.class));
    }

    @GetMapping("/tree")
    @Operation(summary = "获得用户及就诊人")
    public CommonResult<List<BizUserPatientSimpleRespVO>> getBizUser() {
        return success(bizUserService.getBizUserAndPatientTree());
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户分页")
    @PreAuthorize("@ss.hasPermission('business:biz-user:query')")
    public CommonResult<PageResult<BizUserRespVO>> getBizUserPage(@Valid BizUserPageReqVO pageReqVO) {
        PageResult<BizUserDO> pageResult = bizUserService.getBizUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BizUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户 Excel")
    @PreAuthorize("@ss.hasPermission('business:biz-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBizUserExcel(@Valid BizUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BizUserDO> list = bizUserService.getBizUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户.xls", "数据", BizUserRespVO.class,
                        BeanUtils.toBean(list, BizUserRespVO.class));
    }

}